package cl.sirvicod.actionscript.util
{
	import flash.display.DisplayObject;
	
	import mx.core.FlexGlobals;
	import mx.core.UIComponent;
	import mx.events.EffectEvent;
	import mx.events.FlexEvent;
	import mx.managers.PopUpManager;
	
	import spark.effects.Move;
	import spark.effects.Resize;
	import spark.effects.easing.Power;

	public class Movimiento
	{
		private var move:Move = new Move();
		private var moveInv:Move = new Move();
		private var resize:Resize = new Resize();
		private var power:Power = new Power(0.75,3);
		
		private var _height:Number = 0;
		private var _y:Number = 0;
		private var parent:UIComponent;
		
		
		public function Movimiento(){
			this.move.xTo = 0;
			this.move.easer = power;
			this.move.duration = 1000;
			
			this.moveInv.duration = 1000;
			this.moveInv.easer = power;
			
			this.resize.duration = 1000;
		}
		
		public function set y(value:Number):void
		{
			_y = value;
		}
		
		public function set height(value:Number):void
		{
			_height = value;
		}

		/* Funciones de despliegue de componente en pantalla */
		public function abrirComponente(comp:UIComponent, parent:UIComponent=null):void{
			this.parent = parent;
			
			if(!comp.initialized){
				comp.addEventListener(FlexEvent.INITIALIZE, continuarMovimiento);
				comp.initialize();
			}
			else{
			
				if(this._height != 0)
					comp.height = this._height;
				
				if(parent){
					comp.x = (parent.x + parent.width) - comp.width;
					comp.y = parent.y + (parent.height / 2) - (comp.height / 2);
					this.move.xTo = (parent.x + parent.width);
					/*comp.x = (parent.x + parent.width);
					comp.y = parent.y + (parent.height / 2) - (comp.height / 2);
					
					var ancho:Number = comp.width
					resize.widthTo = ancho;
					comp.width = 0;*/
					
					PopUpManager.addPopUp(comp, parent, true);
					
					comp.nestLevel = parent.nestLevel - 1;
					this.move.target = comp;
					this.move.play();
					
					_isOpen = true;
				}
				else{
					comp.x = -comp.width;
					comp.y = this._y;
					
					PopUpManager.addPopUp(comp, FlexGlobals.topLevelApplication as DisplayObject, true);
					
					this.move.target = comp;
					this.move.play();
					
					_isOpen = true;
				}	
			}
		}
		
		private function continuarMovimiento(event:FlexEvent):void{
			var comp:UIComponent = event.currentTarget as UIComponent;
			comp.removeEventListener(FlexEvent.INITIALIZE, continuarMovimiento);
			
			if(this._height != 0)
				comp.height = this._height;
			
			if(parent){
				comp.x = (parent.x + parent.width) - comp.width;
				comp.y = parent.y + (parent.height / 2) - (comp.height / 2);
				this.move.xTo = (parent.x + parent.width);
				/*comp.x = (parent.x + parent.width);
				comp.y = parent.y + (parent.height / 2) - (comp.height / 2);
				
				var ancho:Number = comp.width
				resize.widthTo = ancho;
				comp.width = 0;*/
				
				PopUpManager.addPopUp(comp, parent, true);
				
				comp.nestLevel = parent.nestLevel - 1;
				this.move.target = comp;
				this.move.play();
				
				_isOpen = true;
			}
			else{
				comp.x = -comp.width;
				comp.y = this._y;
				
				PopUpManager.addPopUp(comp, FlexGlobals.topLevelApplication as DisplayObject, true);
				
				this.move.target = comp;
				this.move.play();
				
				_isOpen = true;
			}
		}
		
		public function cerrarComponente(comp:UIComponent):void{
			this.move.xFrom = -comp.width;
			this.move.addEventListener(EffectEvent.EFFECT_END, end_cerrarComponente);
			this.move.play([comp],true);
			
			_isOpen = false;
		}
		
		private function end_cerrarComponente(event:EffectEvent):void{
			event.currentTarget.removeEventListener(EffectEvent.EFFECT_END, end_cerrarComponente);
			PopUpManager.removePopUp(move.target as UIComponent);
		}
		
		
		/* Funciones de despliegue de componente en pantalla INVERTIDO*/
		private var modal:Boolean = true;
		private var xInicio:Number = -1;
		public function abrirComponenteInv(comp:UIComponent, parent:UIComponent=null, modal:Boolean=true, xInicio:Number=-1):void{
			this.parent = parent;
			this.modal = modal;
			this.xInicio = xInicio;
			
			if(!comp.initialized){
				comp.addEventListener(FlexEvent.INITIALIZE, continuarMovimientoInv);
				comp.initialize();
			}
			else{
				
				if(this._height != 0)
					comp.height = this._height;
				
				if(xInicio < 0)
					comp.x = FlexGlobals.topLevelApplication.width;
				else
					comp.x = xInicio;
				comp.y = this._y;
				
				moveInv.xTo = FlexGlobals.topLevelApplication.width - comp.width;
				
				if(!comp.isPopUp)
					PopUpManager.addPopUp(comp, FlexGlobals.topLevelApplication as DisplayObject, modal);
				
				this.moveInv.duration = 1000;
				this.moveInv.play([comp]);
				
				_isOpen = true;
			}
		}
		
		private function continuarMovimientoInv(event:FlexEvent):void{
			var comp:UIComponent = event.currentTarget as UIComponent;
			comp.removeEventListener(FlexEvent.INITIALIZE, cerrarComponenteInv);
			
			if(!comp.isPopUp){
				if(this._height != 0)
					comp.height = this._height;
				
				if(xInicio < 0)
					comp.x = FlexGlobals.topLevelApplication.width;
				else
					comp.x = xInicio;
				comp.y = this._y;
				
				moveInv.xTo = FlexGlobals.topLevelApplication.width - comp.width;
				
				PopUpManager.addPopUp(comp, FlexGlobals.topLevelApplication as DisplayObject, this.modal);
				
				this.moveInv.duration = 1000;
				this.moveInv.target = comp;
				this.moveInv.play();
				
				_isOpen = true;
			}
		}
			
		
		public function cerrarComponenteInv(comp:UIComponent):void{
			this.moveInv.duration = 1000;
			this.moveInv.xFrom = FlexGlobals.topLevelApplication.width - comp.width;
			if(xInicio < 0)
				this.moveInv.xTo = FlexGlobals.topLevelApplication.width;
			else
				this.moveInv.xTo = xInicio;
			this.moveInv.addEventListener(EffectEvent.EFFECT_END, end_cerrarComponenteInv);
			this.moveInv.target = comp;
			this.moveInv.play();
			
			_isOpen = false;
		}
		
		private function end_cerrarComponenteInv(event:EffectEvent):void{
			event.currentTarget.removeEventListener(EffectEvent.EFFECT_END, end_cerrarComponenteInv);
			if(xInicio < 0)
				PopUpManager.removePopUp(moveInv.target as UIComponent);
		}

		
		private var _isOpen:Boolean = false;
		
		public function get isOpen():Boolean
		{
			return _isOpen;
		}
	}
}